package com.bdqn.springbootstudy.controller;

import com.bdqn.springbootstudy.pojo.EbookEntry;
import com.bdqn.springbootstudy.service.EbookEntryService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.soap.SAAJResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/book")
public class EbookController {
    @Resource
    private EbookEntryService ebookEntryService;
    //打开html页面
    @RequestMapping("pageHtml")
    public String openHtml(){
        return "book";
    }
    @RequestMapping("/getAll")
    @ResponseBody
    public Object getAll(){
        Map<String,Object> results=new HashMap<>();
        //获取集合
        List<EbookEntry> allBook = ebookEntryService.getAllBook();
        for (EbookEntry ebookEntry : allBook) {
            System.out.println(ebookEntry);
        }
        results.put("code",0);//成功
        results.put("data",allBook);//用于数据显示
        results.put("count",allBook.size());//决定显示多少页
        return results;
    }
    //打开新增页面
    @RequestMapping("openAddPage")
    public String openAddPage(){
        return "add_page";
    }
    @PostMapping("saveBook")
    @ResponseBody
    public Object saveBook(EbookEntry ebookEntry){
        System.out.println("新增");
        Map<String,Object> map=new HashMap<>();
        int add = ebookEntryService.add(ebookEntry);
        System.out.println(add);
        map.put("code",add);
        return map;
    }
    //删除信息
    @PostMapping("delBook")
    @ResponseBody
    public Object delBook(Integer mainKey){
        System.out.println("删除！");
        Map<String,Object> map=new HashMap<>();
        int del = ebookEntryService.del(mainKey);
        System.out.println(del);
        map.put("code",del);
        return map;
    }
}
